我正在使用一个API,如果它为零,它可以返回一个值,如果它不为零,则返回一个字符串,我想要一个解码器,它将这两个JSON正确解码为结构{"id":1,"rating":0}{"id":2,"rating":"2"}typeUserstruct{Idint64`json:"id"`Ratingint64`json:"rating,string"`}因此它应该尝试将任何JSON类型(int、float、string)转换为struct中指定的类型,并仅在不可能时引发错误。标准json.Decoder不会那样做。或者也许有一些更可定制的json解析库? 最佳答案
给定以下python字典列表:results=[[{'id':'001','result':[0,0,0,0,1]},{'id':'002','result':[1,1,1,1,1]},{'id':'003','result':[0,1,1,None,None]},{'id':'004','result':[0,None,None,1,0]},{'id':'005','result':[1,0,None,1,1]},{'id':'006','result':[0,0,0,1,1]}],[{'id':'001','result':[1,0,1,0,1]},{'id':'002','res
typeApiResponsestruct{Successbool`json:"success"`Errors[]string`json:"errors"`}typeNewSessionResponsestruct{ApiResponse`json:"apiResponse"`authTokenstring`json:"authToken"`}在我的处理程序中,我这样做:resp:=NewSessionResponse{ApiResponse{true,[]string{}},"auth123"}json.NewEncoder(w).Encode(resp)我看到的响应是这样的:{ap